package com.tanhua.dubbo.api.Impl;

import com.itheima.dubbo.api.FriendApi;
import com.tanhua.model.domain.Friend;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.List;

/**
 * @Author Pan Rundong
 * @Date 2023-03-05 10:40
 * @Desc 好友Api实现类
 */
@DubboService
public class FriendApiImpl implements FriendApi {
    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public void save(Long currentUserId, Long friendId) {
        //1.保存自身好友信息
        Query query = Query.query(Criteria.where("userId").is(currentUserId)
                .and("friendId").is(friendId));
        if (!mongoTemplate.exists(query, Friend.class)) {
            Friend friend = new Friend();
            friend.setUserId(currentUserId);
            friend.setFriendId(friendId);
            friend.setCreated(System.currentTimeMillis());
            mongoTemplate.save(friend);
        }

        //2.保存对方好友信息
        Query query2 = Query.query(Criteria.where("userId").is(friendId)
                .and("friendId").is(currentUserId));
        if (!mongoTemplate.exists(query2, Friend.class)) {
            Friend friend = new Friend();
            friend.setUserId(friendId);
            friend.setFriendId(currentUserId);
            friend.setCreated(System.currentTimeMillis());
            mongoTemplate.save(friend);
        }
    }

    /**
     * 根据用户Id查询好友信息
     *
     * @param page
     * @param pagesize
     * @param userId
     * @return java.util.List<com.tanhua.model.domain.Friend>
     * @Author Pan Rundong
     */
    @Override
    public List<Friend> findById(Integer page, Integer pagesize, Long userId) {
        Query query = Query.query(Criteria.where("userId").is(userId));
        query.skip((page-1)*pagesize).limit(pagesize)
                .with(Sort.by(Sort.Order.desc("created")));
        return mongoTemplate.find(query, Friend.class);
    }

    /**
     * 删除联系人
     *
     * @param currentUserId
     * @param friendId
     * @return void
     * @Author Pan Rundong
     */
    @Override
    public void delete(Long currentUserId, Long friendId) {
        //1.删除自身好友信息
        Query query = Query.query(Criteria.where("userId").is(currentUserId)
                .and("friendId").is(friendId));
        if (mongoTemplate.exists(query, Friend.class)) {
            Friend friend = new Friend();
            friend.setUserId(currentUserId);
            friend.setFriendId(friendId);
            friend.setCreated(System.currentTimeMillis());
            mongoTemplate.remove(friend);
        }

        //2.删除对方好友信息
        Query query2 = Query.query(Criteria.where("userId").is(friendId)
                .and("friendId").is(currentUserId));
        if (mongoTemplate.exists(query2, Friend.class)) {
            Friend friend = new Friend();
            friend.setUserId(friendId);
            friend.setFriendId(currentUserId);
            friend.setCreated(System.currentTimeMillis());
            mongoTemplate.remove(friend);
        }
    }
}
